﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;

namespace DALL
{
    public class DBHelper
    {
        public static readonly string connectionString = ConfigurationManager.ConnectionStrings["Connection String"].ToString();
        public static void ExecuteSqlTran(ArrayList SQLStringList)
        {
           // bool bResult = true;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                //开始数据库事务
                SqlTransaction trans = conn.BeginTransaction();
                //设置将在其中执行 SqlCommand 的 SqlTransaction
                cmd.Transaction = trans;
                try
                {
                    for (int n = 0; n < SQLStringList.Count; n++)
                    {
                        string strsql = SQLStringList[n].ToString();
                        if (strsql != null)
                        {
                            if (strsql.Trim().Length > 1)
                            {
                                //设置要对数据源执行的 Transact-SQL 语句
                                cmd.CommandText = strsql;
                                //对连接执行 Transact-SQL 语句
                                cmd.ExecuteNonQuery();
                            }
                        }
                    }
                    //提交数据库事务
                    trans.Commit();
                }
                catch (System.Data.SqlClient.SqlException E)
                {
                    //当出现异常时，从挂起状态回滚事务
                    trans.Rollback();
                    conn.Close();
                  //  bResult = false;
                    throw new Exception(E.Message);
                }
                finally
                {
                    conn.Close();
                }
                //return bResult;
            }
        }

    }
}
